package com.zx.mes.coolmq.hyl.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.zx.mes.coolmq.hyl.sender.RabbitmqSender;
import com.zx.mes.coolmq.hyl.util.MqConstants;
import com.zx.mes.coolmq.hyl.util.RabbitMetaMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 对分布式事务进行测试
 * @author hyl
 * @date 2018-3-20
 * @version 1.0.0
 */
@RestController
public class MessageProviderController {

    @Autowired
    private RabbitmqSender rabbitmqSender;

    @GetMapping("/sender")
    @Transactional
    public String sender(){
        /** 生成一个发送对象 */
        RabbitMetaMessage rabbitMetaMessage = new RabbitMetaMessage();
        /** 设置交换机 */
        rabbitMetaMessage.setExchange(MqConstants.BUSINESS_EXCHANGE);

        /** 指定routing key */
        rabbitMetaMessage.setRoutingKey(MqConstants.BUSINESS_KEY);

        /** 设置需要传送的消息体，可以是任意对象 */
        rabbitMetaMessage.setPayload("你想传递的消息");

        try {
            rabbitmqSender.send(rabbitMetaMessage);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return "error";
        }
        return "success";
    }
}
